package com.k.base.jdk.stream.listquchongfu;

import com.k.base.jdk.stream.CollectionUtil;
import com.k.base.jdk.stream.listbean.User;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * list去重
 * 在Java中，可以使用Stream API的distinct()方法根据某个字段去重，但是distinct()方法只能用于去除完全相等的对象。
 * 如果需要根据某个字段去重，通常的做法是先通过
 * Stream的collect()方法结合Collectors.toMap()收集器来创建一个Map，
 * 然后再从Map的value集合中提取出去重后的对象。
 */
public class ListQuChongFu {
    public static void main(String[] args) {
        List<Person> list = Arrays.asList(
                new Person("1", 22,"Alice","tag1"),
                new Person("2", 24,"Bob","tag2"),
                new Person("1", 22,"Alice","tag3"),
                new Person("3", 25,"Charlie","tag4")
        );
        quChongfu2(list);
//        findChongfu(list);
        //List<Person> dl = CollectionUtil.getDistinctList(list,a-> a.getAge()+"_"+a.getName());
//        System.out.println(dl);
    }
    // list去重, bean 必须实现 equal和hash方法才可以
    private static void quChongfu(List<Person> list){
        List<Person> distinctList = list.stream().distinct().collect(Collectors.toList());
    }
    // list去重, 根据 1个字段去重
    private static void quChongfu1(List<Person> list){
        List<Person> distinctPeople = list.stream()
                .collect(Collectors.collectingAndThen(
                        Collectors.toMap(Person::getId, Function.identity(), (p1, p2) -> p1),
                        map -> new ArrayList<>(map.values())
                ));
        distinctPeople.forEach(System.out::println);
    }
    // list去重, 根据2个字段去重
    private static void quChongfu2(List<Person> list){
        List<Person> distinctUsers = list.stream()
                .collect(Collectors.collectingAndThen(
                        Collectors.toMap(obj->obj.getName()+"_"+obj.getAge(), Function.identity(), (p1, p2) -> p1),
                        map -> new ArrayList<>(map.values())
                ));
        distinctUsers.forEach(System.out::println);
    }

    // list找出重复记录, 根据1个字段
    private static void findChongfu(List<Person> list){
        // 找出 List 重复记录
        List<String> filterList = list.stream()
                .filter(a -> a.getName() != null)
                .collect(Collectors.groupingBy(Person::getName, Collectors.counting()))
                .entrySet()
                .stream()
                .filter(c -> c.getValue() > 1)
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
        for (String uu : filterList) {
            System.out.println("Duplicate name:" + uu);
        }
    }

    static class Person{
        private String id;
        private Integer age;
        private String name;
        private String tag;

        public Person(String id, Integer age,String name,String tag) {
            this.id = id;
            this.age = age;
            this.name = name;
            this.tag = tag;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getTag() {
            return tag;
        }

        public void setTag(String tag) {
            this.tag = tag;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "id='" + id + '\'' +
                    ", age=" + age +
                    ", name='" + name + '\'' +
                    ", tag='" + tag + '\'' +
                    '}';
        }
    }
}
